<template>
  <view class="notification-detail-container">
    <view class="header-section">
      <text class="page-title">通知详情</text>
    </view>
    
    <view class="notification-card">
      <view class="notification-header">
        <text class="notification-title">{{ notification.title }}</text>
        <text class="notification-time">{{ formatTime(notification.createdAt) }}</text>
      </view>
      
      <view class="notification-content">
        <text class="content-text">{{ notification.content }}</text>
      </view>
      
      <view class="notification-meta" v-if="notification.relatedType">
        <text class="meta-label">相关类型：</text>
        <text class="meta-value">{{ getRelatedTypeText(notification.relatedType) }}</text>
      </view>
    </view>
    
    <view class="action-section">
      <button class="back-button" @click="goBack">返回</button>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { notificationApi } from '../../../src/services/api'

// 通知详情数据
const notification = ref({
  id: 0,
  title: '',
  content: '',
  isRead: false,
  relatedType: '',
  relatedId: 0,
  createdAt: ''
})

// 页面加载时获取通知详情
onMounted(() => {
  // 从路由参数获取通知ID
  const pages = getCurrentPages()
  const current = pages[pages.length - 1]
  // @ts-ignore
  const options = current.options || {}
  const notificationId = options.id ? parseInt(options.id) : 1
  fetchNotificationDetail(notificationId)
})

// 获取通知详情
const fetchNotificationDetail = async (id: number) => {
  try {
    const response: any = await notificationApi.getNotificationDetail(id)
    notification.value = response.data
    
    // 标记为已读
    if (!notification.value.isRead) {
      await notificationApi.markAsRead(id)
    }
  } catch (error) {
    console.error('获取通知详情失败:', error)
    uni.showToast({
      title: '获取通知详情失败',
      icon: 'none'
    })
  }
}

// 格式化时间
const formatTime = (time: string) => {
  return time ? time.split('T')[0] : ''
}

// 获取相关类型文本
const getRelatedTypeText = (type: string) => {
  switch (type) {
    case 'course': return '课程'
    case 'booking': return '预约'
    case 'checkin': return '签到'
    case 'plan': return '健身计划'
    default: return '其他'
  }
}

// 返回上一页
const goBack = () => {
  uni.navigateBack()
}
</script>

<style lang="scss" scoped>
.notification-detail-container {
  min-height: 100vh;
  background-color: #FFFFFF;
  padding: 20rpx;
}

.header-section {
  background-color: #fff;
  padding: 30rpx;
  margin-bottom: 20rpx;
  border-radius: 10rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
  
  .page-title {
    font-size: 36rpx;
    font-weight: bold;
    color: #333;
    text-align: center;
  }
}

.notification-card {
  background-color: #fff;
  border-radius: 10rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
}

.notification-header {
  display: flex;
  flex-direction: column;
  gap: 15rpx;
  margin-bottom: 30rpx;
  padding-bottom: 20rpx;
  border-bottom: 2rpx solid #f5f5f5;
}

.notification-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
}

.notification-time {
  font-size: 24rpx;
  color: #999;
}

.notification-content {
  margin-bottom: 30rpx;
}

.content-text {
  font-size: 28rpx;
  color: #666;
  line-height: 1.5;
}

.notification-meta {
  display: flex;
  align-items: center;
}

.meta-label {
  font-size: 28rpx;
  color: #666;
}

.meta-value {
  font-size: 28rpx;
  color: #333;
  font-weight: bold;
}

.action-section {
  padding: 0 20rpx;
}

.back-button {
  background-color: #4B0082;
  color: #fff;
  border: none;
  padding: 30rpx;
  border-radius: 10rpx;
  font-size: 32rpx;
  width: 100%;
}
</style>